<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Boot 内嵌服务器深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        /* 首字下沉效果 */
        .drop-cap::first-letter {
            float: left;
            font-size: 4.5rem;
            line-height: 3.5rem;
            padding-right: 0.5rem;
            margin-top: 0.25rem;
            font-weight: 700;
            color: #4f46e5;
            font-family: 'Noto Serif SC', serif;
        }
        
        /* 代码块美化 */
        pre {
            background: #1e293b;
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            position: relative;
        }
        
        pre::before {
            content: '';
            position: absolute;
            top: 0.75rem;
            left: 1rem;
            width: 12px;
            height: 12px;
            background: #ff5f56;
            border-radius: 50%;
            box-shadow: 20px 0 0 #ffbd2e, 40px 0 0 #27c93f;
        }
        
        code {
            color: #e2e8f0;
            font-family: 'Fira Code', 'Consolas', monospace;
            font-size: 0.9rem;
            line-height: 1.6;
        }
        
        /* 内联代码样式 */
        .inline-code {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 0.125rem 0.375rem;
            border-radius: 4px;
            font-family: 'Fira Code', 'Consolas', monospace;
            font-size: 0.875rem;
            font-weight: 500;
        }
        
        /* 卡片悬停效果 */
        .feature-card {
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }
        
        .feature-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
        }
        
        /* 标题装饰线 */
        .section-title::after {
            content: '';
            display: block;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #4f46e5 0%, #7c3aed 100%);
            margin-top: 1rem;
            border-radius: 2px;
        }
        
        /* 引用块样式 */
        .quote-block {
            position: relative;
            padding-left: 2rem;
        }
        
        .quote-block::before {
            content: '"';
            position: absolute;
            left: -0.5rem;
            top: -1rem;
            font-size: 4rem;
            color: #e0e7ff;
            font-family: 'Noto Serif SC', serif;
        }
        
        /* 列表项美化 */
        .custom-list li {
            position: relative;
            padding-left: 2rem;
            margin-bottom: 1rem;
        }
        
        .custom-list li::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0.5rem;
            width: 8px;
            height: 8px;
            background: #4f46e5;
            border-radius: 50%;
            box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.2);
        }
        
        /* 滚动动画 */
        .fade-in {
            opacity: 0;
            transform: translateY(20px);
            animation: fadeIn 0.8s ease-out forwards;
        }
        
        @keyframes fadeIn {
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        /* Mermaid 图表样式 */
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        }
    </style>
</head>
<body>
    <!-- Hero 区域 -->
    <section class="relative overflow-hidden bg-gradient-to-br from-indigo-600 via-purple-600 to-pink-500 text-white">
        <div class="absolute inset-0 bg-black opacity-20"></div>
        <div class="relative container mx-auto px-6 py-24 text-center">
            <h1 class="text-5xl md:text-7xl font-bold mb-6 animate-pulse">
                Spring Boot 内嵌服务器
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                深入解析 Web 服务器集成机制与源码实现
            </p>
            <div class="flex justify-center space-x-6 text-sm">
                <span class="flex items-center">
                    <i class="fas fa-server mr-2"></i>
                    Tomcat / Jetty / Undertow
                </span>
                <span class="flex items-center">
                    <i class="fas fa-code mr-2"></i>
                    源码解析
                </span>
                <span class="flex items-center">
                    <i class="fas fa-rocket mr-2"></i>
                    自动配置
                </span>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0">
            <svg viewBox="0 0 1440 120" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 120L60 105C120 90 240 60 360 45C480 30 600 30 720 37.5C840 45 960 60 1080 67.5C1200 75 1320 75 1380 75L1440 75V120H1380C1320 120 1200 120 1080 120C960 120 840 120 720 120C600 120 480 120 360 120C240 120 120 120 60 120H0Z" fill="white"/>
            </svg>
        </div>
    </section>

    <!-- 主要内容区域 -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- 引言部分 -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-12 fade-in">
            <p class="text-lg leading-relaxed text-gray-700 drop-cap">
                在传统的 Java Web 开发中，Web 应用通常部署在外部的容器（如 Tomcat、Jetty 或 Undertow）中。但在 Spring Boot 中，Web 服务器被集成到应用中，成为内嵌服务器，直接在 Spring Boot 应用启动时启动。这种方式可以简化开发和部署过程，减少了开发人员的运维成本。
            </p>
        </section>

        <!-- 核心概念可视化 -->
        <section class="bg-gradient-to-r from-purple-50 to-indigo-50 rounded-2xl p-8 mb-12 fade-in">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 section-title">
                <i class="fas fa-sitemap mr-3 text-indigo-600"></i>
                内嵌服务器架构概览
            </h2>
            <div class="mermaid">
                graph TB
                    A[SpringApplication] -->|启动| B{WebApplicationType}
                    B -->|SERVLET| C[EmbeddedWebApplicationContext]
                    B -->|REACTIVE| D[ReactiveWebApplicationContext]
                    B -->|NONE| E[非Web应用]
                    
                    C --> F[WebServerFactory选择]
                    F --> G[TomcatServletWebServerFactory]
                    F --> H[JettyWebServerFactory]
                    F --> I[UndertowWebServerFactory]
                    
                    G --> J[创建Tomcat实例]
                    H --> K[创建Jetty实例]
                    I --> L[创建Undertow实例]
                    
                    J --> M[启动内嵌服务器]
                    K --> M
                    L --> M
                    
                    style A fill:#4f46e5,stroke:#333,stroke-width:2px,color:#fff
                    style M fill:#10b981,stroke:#333,stroke-width:2px,color:#fff
            </div>
        </section>

        <!-- 核心类解析 -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold text-gray-800 mb-8 section-title fade-in">
                <i class="fas fa-cube mr-3 text-indigo-600"></i>
                1. Spring Boot 内嵌服务器的核心类
            </h2>
            
            <div class="bg-white rounded-2xl shadow-lg p-8 mb-8 fade-in">
                <p class="text-lg leading-relaxed text-gray-700 mb-6">
                    Spring Boot 支持的内嵌服务器有多个：Tomcat、Jetty 和 Undertow。<span class="inline-code">SpringApplication</span> 类通过 <span class="inline-code">EmbeddedWebApplicationContext</span> 启动和配置内嵌服务器。内嵌服务器的配置和启动通常是在 Spring Boot 的自动配置机制下进行的，主要通过 <span class="inline-code">SpringBootServletInitializer</span> 和 <span class="inline-code">EmbeddedServletContainerFactory</span> 相关类来完成。
                </p>
                
                <h3 class="text-2xl font-semibold text-gray-800 mb-4">
                    <i class="fas fa-play-circle mr-2 text-purple-600"></i>
                    SpringApplication 启动时选择内嵌服务器
                </h3>
                
                <p class="text-gray-700 mb-6">
                    <span class="inline-code">SpringApplication</span> 是 Spring Boot 启动的核心类，它负责创建应用上下文、启动嵌入式 Web 服务器以及执行自动配置。通过 <span class="inline-code">run()</span> 方法启动时，<span class="inline-code">SpringApplication</span> 会自动判断应该使用哪个内嵌服务器。
                </p>
                
                <pre><code>public ConfigurableApplicationContext run(String... args) {
    // ... 省略其他代码
    // 判断 WebApplicationType 类型，决定是否需要启动内嵌服务器
    if (this.webApplicationType == WebApplicationType.SERVLET) {
        // Web应用类型为SERVLET，启动内嵌服务器
        return runWebApplication(args);
    }
    // ... 省略其他代码
}</code></pre>
            </div>
        </section>

        <!-- Web应用类型 -->
        <section class="mb-12">
            <h3 class="text-2xl font-semibold text-gray-800 mb-6 fade-in">
                <i class="fas fa-filter mr-2 text-purple-600"></i>
                选择内嵌服务器的条件判断
            </h3>
            
            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="bg-white rounded-xl shadow-lg p-6 feature-card fade-in">
                    <div class="text-4xl mb-4 text-center">
                        <i class="fas fa-times-circle text-gray-500"></i>
                    </div>
                    <h4 class="text-xl font-semibold text-center mb-3">NONE</h4>
                    <p class="text-gray-600 text-center">非 Web 应用，不启动内嵌服务器</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 feature-card fade-in" style="animation-delay: 0.1s">
                    <div class="text-4xl mb-4 text-center">
                        <i class="fas fa-server text-indigo-600"></i>
                    </div>
                    <h4 class="text-xl font-semibold text-center mb-3">SERVLET</h4>
                    <p class="text-gray-600 text-center">Servlet 类型的 Web 应用，启动嵌入式 Tomcat、Jetty 或 Undertow</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 feature-card fade-in" style="animation-delay: 0.2s">
                    <div class="text-4xl mb-4 text-center">
                        <i class="fas fa-bolt text-purple-600"></i>
                    </div>
                    <h4 class="text-xl font-semibold text-center mb-3">REACTIVE</h4>
                    <p class="text-gray-600 text-center">响应式 Web 应用，启动嵌入式 WebFlux 服务</p>
                </div>
            </div>
        </section>

        <!-- Tomcat 配置详解 -->
        <section class="mb-12">
            <h2 class="text-3